<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<div th:insert="/common/common"></div>
<style>
    .iframeClass{
        height: 800px !important;
        width: 1000px !important;
    }
</style>
<script type="text/javascript">
    var setting = {
        check: {
            enable: true,
            autoCheckTrigger: true
        },
        async: {
            enable: true,
            dataType: "json",
            url: "/menucontroller/getzTree",
            autoParam: ["id", "name"]
        },
        view : {
            addHoverDom: function (treeId, treeNode) {
                var aObj = $("#" + treeNode.tId + "_a");
                if (treeNode.editNameFlag || $("#btnGroup" + treeNode.tId).length > 0) return;
                var s = '<span id="btnGroup' + treeNode.tId + '">';
                if (treeNode.level == 0) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >添加</a>';
                } else if (treeNode.level == 1) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">修改</a>';
                    if (treeNode.children == undefined) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#" >删除</a>';
                    }
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >添加</a>';
                } else if (treeNode.level == 2) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">修改</a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#">删除</a>';
                }
                s += '</span>';
                aObj.after(s);
            },
            removeHoverDom: function (treeId, treeNode) {
                $("#btnGroup" + treeNode.tId).remove();
            }
        }
    };
    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting);
    });

    function table_refrese(){
        // 刷新当前table -> 请求一次
        $("#table").bootstrapTable("refresh");
    }

    //增加
    function addNode(id){
        BootstrapDialog.show({
            title: '增加',
            message: $('<div></div>').load("/menucontroller/toinsert"),
            closeable: true,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                action: function (dialog) {
                    $.ajax({
                        url : "/menucontroller/insertinto",
                        type : "post",
                        dataType : "json",
                        data : $("#treeid").serialize() + "&pid=" + id,
                        async : false,
                        success : function (insert){
                            if (insert){
                                toastr.success("增加成功");
                                dialog.close();
                                $.fn.zTree.init($("#treeDemo"), setting);
                            }
                        },
                        error : function (insert){
                            toastr.error("网络挂掉了，请刷新");
                        }
                    })

                }
            }]
        });
    }

    //修改
    function editNode(id){
        BootstrapDialog.show({
            title: '修改',
            message: $('<div></div>').load("/menucontroller/toupdate?pid=" + id),
            closeable: true,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                action: function (dialog) {
                    $.ajax({
                        url : "/menucontroller/updateinto",
                        type : "post",
                        dataType : "json",
                        data : $("#treeid").serialize(),
                        async : false,
                        success : function (insert){
                            if (insert){
                                toastr.success("修改成功");
                                dialog.close();
                                $.fn.zTree.init($("#treeDemo"), setting);
                            }
                        },
                        error : function (insert){
                            toastr.error("网络挂掉了，请刷新");
                        }
                    })

                }
            }]
        });
    }

    //菜单删除
    function deleteNode(id){
        $.ajax({
            url : "/menucontroller/deletetree?id=" + id,
            type : "post",
            dataType : "json",
            async : false,
            success : function (insert){
                if (insert){
                    toastr.success("修改成功");
                    $.fn.zTree.init($("#treeDemo"), setting);
                }
            },
            error : function (insert){
                toastr.error("网络挂掉了，请刷新");
            }
        })
    }
</script>
<body>
    <div class="form-group">
        <label for="treeDemo" class="col-sm-2 control-label">权限</label>
        <div class="col-sm-10">
            <ul id="treeDemo" class="ztree"></ul>
        </div>
    </div>
</body>
</html>